package com.quanyan.ic.vendingMachine.dao.impl;

import com.quanyan.ic.vendingMachine.dao.VmGridGoodsMappingDAO;
import com.quanyan.ic.vendingMachine.mapper.VmGridGoodsMappingEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.entity.VmGridGoodsMappingEntity;
import com.quanyan.ic.vendingMachine.mapper.entity.VmGridGoodsMappingEntityExample;
import com.quanyan.ic.vendingMachine.model.GoodsInfo;
import com.quanyan.ic.vendingMachine.model.GridGoodsMapping;
import com.quanyan.ic.vendingMachine.model.Image;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by zhuo.shi on 2017/8/1.
 */
@Repository
public class VmGridGoodsMappingDAOImpl implements VmGridGoodsMappingDAO {

    @Autowired
    private VmGridGoodsMappingEntityMapper vmGridGoodsMappingEntityMapper;

    @Override
    public GridGoodsMapping find(String vmNumber, String grid, Byte gridNumber) {
        if (StringUtils.isEmpty(vmNumber) || StringUtils.isEmpty(grid) || gridNumber == null) {
            return null;
        }
        VmGridGoodsMappingEntityExample example = new VmGridGoodsMappingEntityExample();
        example.createCriteria()
                .andVmNumberEqualTo(vmNumber)
                .andGridEqualTo(grid)
                .andGridNumberEqualTo(gridNumber);
        List<VmGridGoodsMappingEntity> entityList = vmGridGoodsMappingEntityMapper.selectByExample(example);
        if (entityList == null || entityList.size() <= 0) {
            return null;
        }
        VmGridGoodsMappingEntity entity = entityList.get(0);
        return toModel(entity);
    }

    @Override
    public void insert(GridGoodsMapping gridGoodsMapping) {
        vmGridGoodsMappingEntityMapper.insertSelective(toEntity(gridGoodsMapping));
    }

    @Override
    public void delete(String vmNumber, String grid, Byte gridNumber) {
        VmGridGoodsMappingEntityExample example = new VmGridGoodsMappingEntityExample();
        example.createCriteria()
                .andVmNumberEqualTo(vmNumber)
                .andGridEqualTo(grid)
                .andGridNumberEqualTo(gridNumber);
        vmGridGoodsMappingEntityMapper.deleteByExample(example);
    }

    private GridGoodsMapping toModel(VmGridGoodsMappingEntity entity) {
        GridGoodsMapping mapping = new GridGoodsMapping();
        BeanUtils.copyProperties(entity, mapping);
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(entity.getGoodsId());
        goodsInfo.setName(entity.getGoodsName());
        goodsInfo.setUuid(entity.getGoodsUuid());
        double price = entity.getGoodsPrice().doubleValue();
        goodsInfo.setPrice(price);
        Image image = new Image();
        image.setImgUrl_S(entity.getGoodsSmallImgUrl());
        image.setImgUrl_M(entity.getGoodsMiddleImgUrl());
        image.setImgUrl_L(entity.getGoodsLargeImgUrl());
        goodsInfo.setImg(image);
        mapping.setGoodsInfo(goodsInfo);
        return mapping;
    }

    private VmGridGoodsMappingEntity toEntity(GridGoodsMapping mapping) {
        VmGridGoodsMappingEntity entity = new VmGridGoodsMappingEntity();
        BeanUtils.copyProperties(mapping, entity);
        GoodsInfo goodsInfo = mapping.getGoodsInfo();
        if (goodsInfo != null) {
            entity.setGoodsCategory(goodsInfo.getCategoryName());
            entity.setGoodsCategoryId(goodsInfo.getCategoryId());
            entity.setGoodsId(goodsInfo.getId());
            entity.setGoodsUuid(goodsInfo.getUuid());
            entity.setGoodsName(goodsInfo.getName());
            entity.setGoodsPrice(new BigDecimal(goodsInfo.getPrice()));
            Image image = goodsInfo.getImg();
            if (image != null) {
                entity.setGoodsSmallImgUrl(image.getImgUrl_S());
                entity.setGoodsMiddleImgUrl(image.getImgUrl_M());
                entity.setGoodsLargeImgUrl(image.getImgUrl_L());
            }
        }
        return entity;
    }
}
